#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import webapp2

webToDeepLink = {'https://www.flixster.com/movie/the-hunger-games/':'flixster://movie?id=771250004'}

linksToDisplay = ['http://externaldeeplink.appspot.com/appnoindexdeeplink',
                  'http://externaldeeplink.appspot.com/workingdeeplink',
                  'http://externaldeeplink.appspot.com/nonworkingdeeplink',]

htmlbody = """
<html> 
<head> 
<link rel="alternate" href="android-app://com.google.calypso.calypsoerror/http/externaldeeplink.appspot.com/{0}" />
</head>
<body>
{1}
</body>
</html>
"""

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(linkify())

class WorkingDeepLinkHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(htmlbody.format('workingdeeplink',
                                            'This is a working deep link.'))

class NonWorkingDeepLinkHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(htmlbody.format('nonworkingdeeplink',
                                            'This deep link will not work with the app.'))

class AppNoIndexDeepLinkHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(htmlbody.format('appnoindexdeeplink',
                                            'This link should be indexed as a web link, but the '
                                            'corresponding deep link in the app should not be indexed.'))

class GiveDeepLinkReflect(webapp2.RequestHandler):
    def get(self):
        weburl = self.request.get('weburl')
        self.response.write(weburl)

class GiveDeepLinkReal(webapp2.RequestHandler):
    def get(self):
        weburl = self.request.get('weburl')
        if weburl in webToDeepLink:
            self.response.write(webToDeepLink[weburl])
        else:
            self.response.write(weburl)

homepage = """
<html>
<head>
<meta name="google-site-verification" content="6pVVL7YUyE9UjxJobBIDn4c2VNEP2J5aRWeumwX4ZCA" />
<title> External deep link </title>
</head> 
<body>
{0}
</body>
</html>
"""

def linkify():
    res = []
    for link in linksToDisplay:
       res.append('<br><h3><a href="{0}">{0}</a></h3></br>'.format(link))
    htmlBody = '\n'.join(res)
    return homepage.format(htmlBody)

app = webapp2.WSGIApplication([
    ('/appnoindexdeeplink', AppNoIndexDeepLinkHandler),
    ('/workingdeeplink', WorkingDeepLinkHandler),
    ('/nonworkingdeeplink', NonWorkingDeepLinkHandler),
    ('/', MainHandler),
    ('/givedeeplink', GiveDeepLinkReflect),
    ('/givedeeplinkreal', GiveDeepLinkReal),
], debug=True)
